package edu.ynu.se.xiecheng.achitectureclass.demo.controller;

import edu.ynu.se.xiecheng.achitectureclass.common.controller.LogicController;
import edu.ynu.se.xiecheng.achitectureclass.demo.dao.CameraDataDao;
import edu.ynu.se.xiecheng.achitectureclass.demo.entity.data.CameraData;
import edu.ynu.se.xiecheng.achitectureclass.demo.service.CameraDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api(tags = "摄像头")
@RestController
@RequestMapping("/api/camera-datas")
@CrossOrigin(origins = "http://localhost:5173", allowedHeaders = "*", allowCredentials = "true")
public class CameraDataController extends LogicController<CameraDataService, CameraDataDao, CameraData, Long> {

    public CameraDataController(@Autowired CameraDataService ls) {
        super(ls);
    }

    @ApiOperation("获取摄像头加载的验证Token")
    @GetMapping("/getToken")
    public ResponseEntity<?> getCameraToken() {
        Map<String, String> token = getService().getCameraToken();
        if (token != null) {
            return ResponseEntity.ok(token);
        }
        else {
            return ResponseEntity.notFound().build();
        }
    }
    @ApiOperation("拍摄一张照片")
    @GetMapping("/capture")
    public ResponseEntity<?> capture() {
        Map<String, String> image = getService().capture();
        if (image != null) {
            return ResponseEntity.ok(image);
        }else {
            return ResponseEntity.notFound().build();
        }
    }
}
